package cn.jxufe.zhangyong.mysqldbapp;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {

    public TextView userName;
    public TextView userPasswd;
    public TextView loginBtn;

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
    public void onResume() {
        super.onResume();
        if(MainActivity.logon==true){
            Intent intent = new Intent(LoginActivity.this,MainActivity.class);
            startActivity(intent);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        userName = findViewById(R.id.username);
        userPasswd = findViewById(R.id.password);
        loginBtn = findViewById(R.id.login);

        userName.addTextChangedListener(textWatcher);
        userPasswd.addTextChangedListener(textWatcher);
    }

    TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (userName.getText().length() == 0 || userPasswd.getText().length() == 0) {
                loginBtn.setEnabled(false);
                Toast.makeText(LoginActivity.this, "按键不可点击", Toast.LENGTH_SHORT).show();
            } else {
                loginBtn.setEnabled(true);
                Toast.makeText(LoginActivity.this, "按键可点击", Toast.LENGTH_SHORT).show();
            }
        }
    };

    public void myLogin(View v) {
        assert userName != null;
        String name = userName.getText().toString();
        String passwd = userPasswd.getText().toString();
        System.out.println("name-->" + name);


        Cursor cursor = MainActivity.myDB.query(MainActivity.MY_TB_USER,
                new String[]{"user, passwd"},
                "user=?", new String[]{name},
                null, null, null);
        if (cursor.getCount()!=0) {
            cursor.moveToFirst();
            toLogin(cursor,name,passwd);
        }
        if (cursor.getCount()==0){
            register(name,passwd);
            userName.setText("");
            userPasswd.setText("");
            display("成功","注册成功！","确定",null);
        }
    }

    public void toLogin( Cursor cursor,String name,String passwd){
        if (cursor.getString(1).equals(passwd)) {
            MainActivity.logon = true;
        } else {
            MainActivity.logon = false;
        }
        if (MainActivity.logon) {
            MainActivity.MY_TB_NAME = name;
            setContentView(R.layout.activity_my_sqldb);
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            startActivity(intent);
            this.finish();
        } else {
            display("密码错误","请重新输入密码？","确定","取消");
        }
        cursor.close();
    }

    //注册
    public void register(String name,String passwd) {
        ContentValues cv = new ContentValues();
        MainActivity.myDB.execSQL("CREATE TABLE IF NOT EXISTS " + name +
                "(_id INTEGER PRIMARY KEY, user TEXT,secret TEXT)");

        cv.put(MainActivity.MY_FD_USER, name);
        cv.put(MainActivity.MY_FD_PASSWD, passwd);
        MainActivity.myDB.insert(MainActivity.MY_TB_USER, null, cv);
    }

    //提示弹窗
    public void display(String title,String count,String yseText,String noText){
        new AlertDialog.Builder(this)
                .setTitle(title)
                .setMessage(count)
                .setPositiveButton(yseText, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // continue
//                        setContentView(R.layout.activity_login);
                    }
                })
                .setNegativeButton(noText, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // do nothing
                    }
                })
                .setIcon(android.R.drawable.ic_dialog_alert)
                .show();
    }


}